<?php
declare(strict_types=1);

namespace LowCode\Abstracts;

use LowCode\Support\HelpJs;
use LowCode\Traits\HasMake;

abstract class JsBase
{

    use HasMake;

    protected string $script = "";

    /**
     * @var bool 是否函数包裹
     */
    protected bool $isFunction = false;

    /**
     * @param string|array $to
     * @return HelpJs
     */
    protected function jsFrom(string|array $to): HelpJs
    {
        return HelpJs::from($to);
    }

    /**
     * @param bool $bool
     * @return $this
     */
    public function setJsFunction(bool $bool = false): static
    {
        $this->isFunction = $bool;
        return $this;
    }

    /**
     * @param $closure
     * @return $this
     */
    protected function callback($closure): static
    {
        if ($closure!=null) $closure($closure,$this);
        return $this;
    }

    protected function toString(): string
    {
        if ($this->isFunction){
            return func($this->script);
        }else{
            return $this->script;
        }
    }
}
